package service;

import dao.TableDao;
import dao.impl.TableDaoImpl;
import org.apache.commons.beanutils.BeanUtils;
import pojo.Table;
import util.LogUtil;
import util.Result;
import util.StringDataCheckUtil;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.sql.Timestamp;
import java.util.List;
import java.util.Map;

public class TableService {

    private TableDao tableDao = new TableDaoImpl();

    /**
     * 添加餐桌
     * @param request
     * @param response
     */
    public void add(HttpServletRequest request, HttpServletResponse response) {
        try {
            Map<String, String[]> parameterMap = request.getParameterMap();
            Table table = new Table();
            BeanUtils.populate(table, parameterMap);
            // 验证餐桌信息是否已经存在
            Integer addInfo = tableDao.checkAddInfo(table);
            if (addInfo == 1) {
                Result.response(1002, "该餐桌名已存在！", response);
                return;
            }else if (addInfo == 2){
                Result.response(1003, "该编号已存在！", response);
                return;
            }
            table.setTableCreateTime(new Timestamp(System.currentTimeMillis()));
            table.setTableUpdateTime(new Timestamp(System.currentTimeMillis()));
            if (tableDao.add(table)>0) {
               Table newTable = tableDao.findNewTable(table);
                if (newTable!=null) {
                    if (tableDao.addTableInfo(newTable.getTableId())!=1) {
                        Result.response(1004, "添加餐桌信息失败！", response);
                        return;
                    }
                }
                LogUtil.addLog("添加了餐桌["+table.getTableName()+"]", request);
                Result.response(200, "添加成功！", response);
                return;
            }
            Result.response(1001, "添加失败！", response);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
    }

    /**
     * 根据餐桌类型查找餐桌编号
     * @param request
     * @param response
     */
    public void findTableCodeByType(HttpServletRequest request, HttpServletResponse response) {
        String tableType = request.getParameter("tableType");
        if (!StringDataCheckUtil.check(tableType)) {
            Result.response(1002, "参数有误！", response);
            return;
        }
        Integer tableCode = tableDao.findTableCodeByType(tableType);
        if (tableCode == null){
            tableCode = 1;
            Result.response(200, "查询成功！", response, tableCode);
            return;
        }
        if (tableCode >0) {
            Result.response(200, "查询成功！", response, tableCode);
            return;
        }
        Result.response(1001, "查询失败！",response);
    }


    /**
     * 根据条件查找餐桌
     * @param request
     * @param response
     */
    public void findTableCodeByParams(HttpServletRequest request, HttpServletResponse response) {
        try {
            Map<String, String[]> parameterMap = request.getParameterMap();
            Table table = new Table();
            BeanUtils.populate(table, parameterMap);
            List<Table> tableCodeByParams = tableDao.findTableCodeByParams(table);
            if (tableCodeByParams ==null) {
                Result.response(1001, "查找失败！", response);
                return;
            }else {
                Result.response(0, "查找成功！", tableDao.countTableCodeByParams(table), response, tableCodeByParams);
            }
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
    }


    /**
     * 软删除
     * @param request
     * @param response
     */
    public void delete(HttpServletRequest request, HttpServletResponse response) {
        String[] ids = request.getParameterValues("ids[]");
        if (ids == null){
            Result.response(1002,"参数不正确!", response);
        }
        Boolean res = false;
        // 遍历ids
        Table table = new Table();
        table.setTableUpdateTime(new Timestamp(System.currentTimeMillis()));
        Integer delete = 0;
        for (String id : ids) {
            delete = tableDao.delete(table, id);
            res = delete > 0 ? true : false;
            if (delete == 2){
                Result.response(1003, "不能删除不处于空闲状态的餐桌！", response);
                return;
            }
        }
        if (res) {
            Result.response(200, "删除成功!", response);
            return;
        }
        Result.response(1001, "删除失败!", response);
    }

    /**
     * 真删除
     * @param request
     * @param response
     */
    public void realDelete(HttpServletRequest request, HttpServletResponse response) {
        String[] ids = request.getParameterValues("ids[]");
        if (ids == null){
            Result.response(1002,"参数不正确!", response);
            return;
        }
        Boolean res = false;
        // 遍历ids
        for (String id : ids) {
            Table editTable = tableDao.findEditTable(id);
            LogUtil.addLog("删除了餐桌["+editTable.getTableName()+"]", request);
            res = tableDao.realDelete(id) > 0 ? true : false;
        }
        if (res) {
            Result.response(200, "删除成功!", response);
            return;
        }
        Result.response(1001, "删除失败!", response);
    }

    /**
     * 编辑餐桌
     * @param request
     * @param response
     */
    public void edit(HttpServletRequest request, HttpServletResponse response) {
        try {
            Map<String, String[]> parameterMap = request.getParameterMap();
            Table table = new Table();
            BeanUtils.populate(table, parameterMap);
            // 检查输入的编辑信息是否已经存在
            Integer checkEditInfo = tableDao.checkEditInfo(table);
            Table editTable = tableDao.findEditTable(table.getTableId().toString());
            if (editTable.getTableStatus()!=0){
                Result.response(1003, "该餐桌处于预约或被使用中不支持修改", response);
                return;
            }
            if (checkEditInfo == 1) {
                Result.response(1001, "该餐桌名已存在！", response);
                return;
            }else if (checkEditInfo == 2){
                Result.response(1002,"相同类型下的编号重复！", response);
                return;
            }
            table.setTableUpdateTime(new Timestamp(System.currentTimeMillis()));
            if (tableDao.edit(table)>0) {
                LogUtil.addLog("编辑了餐桌信息, 餐桌id["+table.getTableId()+"]", request);
                Result.response(200, "编辑成功！", response);
                return;
            }else {
                Result.response(1001, "编辑失败!", response);
            }
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
    }

    /**
     * 编辑餐桌页面
     * @param request
     * @param response
     */
    public void editTablePage(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String tableId = request.getParameter("tableId");
        if (!StringDataCheckUtil.check(tableId)) {
            Result.response(1001, "参数有误！", response);
            return;
        }
        Table editTable = tableDao.findEditTable(tableId);
        if (editTable ==null) {
            Result.response(1002, "没有找到相应餐桌！", response);
            return;
        }
        request.setAttribute("editTable", editTable);
        request.getRequestDispatcher("/WEB-INF/pages/operationManage/tableManage/tableEdit.jsp").forward(request, response);
    }

    /**
     * 恢复
     * @param request
     * @param response
     */
    public void restore(HttpServletRequest request, HttpServletResponse response) {
        String restoreId = request.getParameter("restoreId");
        Boolean res = false;
        res = tableDao.restore(restoreId) > 0 ? true : false;
        if (res) {
            Result.response(200, "恢复成功!", response);
            return;
        }
        Result.response(1001, "恢复失败!", response);
    }
}
